package com.erlang.springbootinit.controller;

import com.erlang.springbootinit.common.JsonData;
import com.erlang.springbootinit.job.AsyncTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Future;

/**
 * Created by IntelliJ IDEA.
 *
 * @author ZGQ
 */
@RestController
@RequestMapping("/api/v1")
public class AsyncController {
    @Autowired
    private AsyncTask task;

    @GetMapping("async_task")
    public JsonData exeTask() throws InterruptedException{

        long begin = System.currentTimeMillis();

        Future<String> task4 = task.task4();
        Future<String> task5 = task.task5();
        Future<String> task6 = task.task6();

        //如果都执行往就可以跳出循环,isDone方法如果此任务完成，true
        for(;;){
            if (task4.isDone() && task5.isDone() && task6.isDone()) {
                break;
            }
        }

        long end = System.currentTimeMillis();
        long total = end-begin;
        System.out.println("执行总耗时="+total);
        return JsonData.buildSuccess(total);
    }
}
